package com.icebartech.core.controller;

import com.icebartech.core.vo.BaseData;
import com.icebartech.core.vo.PageData;
import com.icebartech.core.vo.RespJson;
import com.icebartech.core.vo.ResponseData;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.data.domain.Page;


/**
 * 所有controller类的基类
 * @author Administrator
 *
 */
public class BaseController {
    
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    
    public HttpServletRequest getRequest() {
        return request;
    }
    
    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }
    
    public HttpServletResponse getResponse() {
        return response;
    }
    
    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }
    
    /**
     * 请求失败返回json封装
     * @param msg
     * @return
     */
    public <E> RespJson<E> getFailRtnMap(String msg) {
        RespJson<E> respJson = new RespJson<>();
        respJson.setStatus(500);
        respJson.setMsg(msg);
        return respJson;
    }
    
    /**
     * 请求成功返回json封装（非分页请求）
     * @param msg 提示信息
     * @param baseData 业务数据对象
     * @return
     */
    public <E> RespJson<BaseData<E>> getSuccessRtnMap(String msg, BaseData<E> baseData) {
        RespJson<BaseData<E>> respJson = new RespJson<>();
        respJson.setStatus(200);
        respJson.setMsg(msg);
        if (baseData != null) {
            // Map<String, Object> dataMap = new HashMap<>();
            // dataMap.put("bussData", baseData.getBussData());
            respJson.setData(baseData);
        } else {
            respJson.setData(new BaseData<>());
        }
        return respJson;
    }
    
    /**
     * 请求成功返回json封装（分页请求）
     * @param msg 提示信息
     * @param pageData 分页业务数据对象
     * @return
     */
    public <E> RespJson<PageData<List<E>>> getSuccessPageRtnMap(String msg, PageData<List<E>> pageData) {
        RespJson<PageData<List<E>>> respJson = new RespJson<>();
        respJson.setStatus(200);
        respJson.setMsg(msg);
        if (pageData != null) {
            // Map<String, Object> dataMap = new HashMap<>();
            // dataMap.put("count", pageData.getCount());
            // dataMap.put("pageCount", pageData.getPageCount());
            // dataMap.put("bussData", pageData.getBussData());
            // dataMap.put("ext", pageData.getExt());
            respJson.setData(pageData);
        } else {
            respJson.setData(new PageData<>());
        }
        return respJson;
    }
    
    /**
     * 自定义status的返回json封装
     * @param status 返回状态码
     * @param msg 提示信息
     * @param baseData 业务数据对象
     * @return
     */
    public <E> RespJson<BaseData<E>> getNormalRtnMap(int status, String msg, BaseData<E> baseData) {
        RespJson<BaseData<E>> respJson = new RespJson<>();
        respJson.setStatus(status);
        respJson.setMsg(msg);
        if (baseData != null) {
            // Map<String, Object> dataMap = new HashMap<>();
            // dataMap.put("bussData", baseData.getBussData());
            respJson.setData(baseData);
        } else {
            respJson.setData(new BaseData<>());
        }
        return respJson;
    }
    
    /**
     * 根据传入ResponseData自动判断返回SUCCESS/FAIL 暂不支持PageInfo分页类型数据
     * @param responseData
     * @param <E>
     * @return
     */
    public <E> RespJson<BaseData<E>> getRtnMap(ResponseData<E> responseData) {
        if (null == responseData) return this.getFailRtnMap("操作失败，未知错误！");
        
        String rtnMsg = responseData.getMessage();
        if (responseData.isSusscess()) {
            return this.getSuccessRtnMap(rtnMsg, new BaseData<>(responseData.getData()));
        } else {
            return this.getFailRtnMap(rtnMsg);
        }
    }
    
    public <E> RespJson<PageData<List<E>>> getPageRtnMap(ResponseData<Page<E>> responseData) {
        if (null == responseData) return this.getFailRtnMap("操作失败，未知错误！");
        
        String rtnMsg = responseData.getMessage();
        if (responseData.isSusscess()) {
            Page<E> page = responseData.getData();
            return this.getSuccessPageRtnMap(rtnMsg, new PageData<>((int) page.getTotalElements(), page.getTotalPages(), page.getContent()));
        } else {
            return this.getFailRtnMap(rtnMsg);
        }
    }
    
    /**
     * 获得系统访问的前缀地址，如：http://www.xxx.com:8080/
     * @return
     */
    public String getBasePath() {
        String path     = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
        return basePath;
    }
    
}
